#include "createmapdialog.h"
#include "ui_createmapdialog.h"

CreateMapDialog::CreateMapDialog(QWidget *parent) :
    QDialog(parent), ui(new Ui::CreateMapDialog)
{
    ui->setupUi(this);
    QRegExp regExpLatitude("-?((90)|(([1-8]?[0-9]?)(\\056)[0-9]{0,10}))");                      // -90 ... 90
    QRegExp regExpLongitude("-?((180)|(((1[0-7][0-9])|([1-9]?[0-9]?))(\\056)[0-9]{0,10}))");    // -180 ... 180
    ui->topLeftLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
    ui->topLeftLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
    ui->topRightLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
    ui->topRightLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
    ui->bottomRightLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
    ui->bottomRightLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
    ui->bottomLeftLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
    ui->bottomLeftLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
    setFixedSize(size());
    // ТЕСТИРОВАНИЕ
    /*
    //Юго-Запад
    ui->topLeftLatLineEdit->setText("59.895571");
    ui->topLeftLongLineEdit->setText("30.082165");
    ui->topRightLatLineEdit->setText("59.895571");
    ui->topRightLongLineEdit->setText("30.253827");
    ui->bottomRightLatLineEdit->setText("59.809358");
    ui->bottomRightLongLineEdit->setText("30.253827");
    ui->bottomLeftLatLineEdit->setText("59.809358");
    ui->bottomLeftLongLineEdit->setText("30.082165");
    */
    /*
    //Северо-Восток
    ui->topLeftLatLineEdit->setText("60.031034");
    ui->topLeftLongLineEdit->setText("30.408127");
    ui->topRightLatLineEdit->setText("60.031034");
    ui->topRightLongLineEdit->setText("30.429585");
    ui->bottomRightLatLineEdit->setText("60.020314");
    ui->bottomRightLongLineEdit->setText("30.429585");
    ui->bottomLeftLatLineEdit->setText("60.020314");
    ui->bottomLeftLongLineEdit->setText("30.408127");
    */

    //Военмех
    ui->topLeftLatLineEdit->setText("59.919645");
    ui->topLeftLongLineEdit->setText("30.307299");
    ui->topRightLatLineEdit->setText("59.919645");
    ui->topRightLongLineEdit->setText("30.318027");
    ui->bottomRightLatLineEdit->setText("59.914267");
    ui->bottomRightLongLineEdit->setText("30.318027");
    ui->bottomLeftLatLineEdit->setText("59.914267");
    ui->bottomLeftLongLineEdit->setText("30.307299");

    // ТЕСТИРОВАНИЕ
}

CreateMapDialog::~CreateMapDialog()
{
    if(ui)  delete ui;
}

void CreateMapDialog::on_openFileButton_clicked(void)
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Выбор изображения для создания карты"), "", tr("*.tif *.gif *.png *.jpg *.svg"));
    ui->pathLineEdit->setText(fileName);
}

bool CreateMapDialog::Result(Map *aMap, bool &aChangeCurrentMap) const
{
    if(IsEnabledToSave() && aMap)
    {
        aMap->ClearMap();
        aMap->SetName(ui->mapNameLineEdit->text());
        aMap->SetFilePath(ui->pathLineEdit->text());
        aMap->SetCalibration(GeoCoordinates(ui->topLeftLatLineEdit->text().toDouble(),        ui->topLeftLongLineEdit->text().toDouble()),
                             GeoCoordinates(ui->topRightLatLineEdit->text().toDouble(),       ui->topRightLongLineEdit->text().toDouble()),
                             GeoCoordinates(ui->bottomRightLatLineEdit->text().toDouble(),    ui->bottomRightLongLineEdit->text().toDouble()),
                             GeoCoordinates(ui->bottomLeftLatLineEdit->text().toDouble(),     ui->bottomLeftLongLineEdit->text().toDouble()));
        aChangeCurrentMap = ui->setCurrentCheckBox->isChecked();
        return true;
    }
    return false;
}

void CreateMapDialog::on_pathLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_topLeftLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_topLeftLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_topRightLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_topRightLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_bottomRightLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_bottomRightLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_bottomLeftLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_bottomLeftLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void CreateMapDialog::on_mapNameLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}


bool CreateMapDialog::CheckCoordinates(void) const
{
    GeoCoordinates topLeft(ui->topLeftLatLineEdit->text().toDouble(), ui->topLeftLongLineEdit->text().toDouble()),
                   topRight(ui->topRightLatLineEdit->text().toDouble(),       ui->topRightLongLineEdit->text().toDouble()),
                   bottomRight(ui->bottomRightLatLineEdit->text().toDouble(), ui->bottomRightLongLineEdit->text().toDouble()),
                   bottomLeft(ui->bottomLeftLatLineEdit->text().toDouble(), ui->bottomLeftLongLineEdit->text().toDouble());
    return (topLeft.GetLatitude() > bottomLeft.GetLatitude()    && topLeft.GetLatitude() > bottomRight.GetLatitude() &&
            topRight.GetLatitude() > bottomLeft.GetLatitude()   && topRight.GetLatitude() > bottomRight.GetLatitude() &&
            bottomRight.GetLongitude() > topLeft.GetLongitude() && bottomRight.GetLongitude() > bottomLeft.GetLongitude() &&
            topRight.GetLongitude() > topLeft.GetLongitude()    && topRight.GetLongitude() > bottomLeft.GetLongitude());
}

bool CreateMapDialog::IsEnabledToSave(void) const
{
    return  (!ui->mapNameLineEdit->text().isEmpty() && !ui->pathLineEdit->text().isEmpty() &&
            (!ui->topLeftLatLineEdit->text().isEmpty() && ui->topLeftLatLineEdit->text().compare(".") &&
            ui->topLeftLatLineEdit->text().compare("-") && ui->topLeftLatLineEdit->text().compare("-.")) &&
            (!ui->topLeftLongLineEdit->text().isEmpty() && ui->topLeftLongLineEdit->text().compare(".") &&
            ui->topLeftLongLineEdit->text().compare("-") && ui->topLeftLongLineEdit->text().compare("-.")) &&
            (!ui->topRightLatLineEdit->text().isEmpty() && ui->topRightLatLineEdit->text().compare(".") &&
            ui->topRightLatLineEdit->text().compare("-") && ui->topRightLatLineEdit->text().compare("-.")) &&
            (!ui->topRightLongLineEdit->text().isEmpty() && ui->topRightLongLineEdit->text().compare(".") &&
            ui->topRightLongLineEdit->text().compare("-") && ui->topRightLongLineEdit->text().compare("-.")) &&
            (!ui->bottomRightLatLineEdit->text().isEmpty() && ui->bottomRightLatLineEdit->text().compare(".") &&
            ui->bottomRightLatLineEdit->text().compare("-") && ui->bottomRightLatLineEdit->text().compare("-.")) &&
            (!ui->bottomRightLongLineEdit->text().isEmpty() && ui->bottomRightLongLineEdit->text().compare(".") &&
            ui->bottomRightLongLineEdit->text().compare("-") && ui->bottomRightLongLineEdit->text().compare("-.")) &&
            (!ui->bottomLeftLatLineEdit->text().isEmpty() && ui->bottomLeftLatLineEdit->text().compare(".") &&
            ui->bottomLeftLatLineEdit->text().compare("-") && ui->bottomLeftLatLineEdit->text().compare("-.")) &&
            (!ui->bottomLeftLongLineEdit->text().isEmpty() && ui->bottomLeftLongLineEdit->text().compare(".") &&
            ui->bottomLeftLongLineEdit->text().compare("-") && ui->bottomLeftLongLineEdit->text().compare("-.")) &&
            CheckCoordinates());
}
